package com.vpx.server.mapper.system;

import com.vpx.server.bean.entity.SysDicValue;
import com.vpx.server.bean.info.system.SysDicValueInfo;
import com.vpx.server.bean.order.system.SysDicValuePageOrder;
import com.vpx.server.framework.base.BaseMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author liuks
 * @title: vpx
 * @module: 数据字典
 * @description: 数据字典Mapper
 * @date 2021/09/20 18:34
 */
public interface SysDicValueMapper extends BaseMapper<SysDicValue> {

    /**
     * 根据id查询数据字典
     *
     * @param id
     * @return
     */
    @Select("select * from sys_dic_value where status = 'ABLE' and id = #{id}")
    SysDicValue querySysDicValueById(@Param("id") Long id);

    /**
     * 根据类型id修改数据字典状态
     *
     * @param typeId
     * @param status
     * @return
     */
    @Update("update sys_dic_value set status = #{status} where type_id = #{typeId}")
    int updateSysDicValueByTypeId(@Param("typeId") Long typeId, @Param("status") String status);

    /**
     * 根据id修改数据字典状态
     *
     * @param id
     * @param status
     * @return
     */
    @Update("update sys_dic_value set status = #{status} where id = #{id}")
    int updateSysDicValueById(@Param("id") Long id, @Param("status") String status);

    /**
     * 根据类型ID查询数据字典列表
     *
     * @param typeId
     * @return
     */
    @Select("select * from sys_dic_value where status = 'ABLE' and type_id = #{typeId}")
    List<SysDicValueInfo> queryDicValueListByTypeId(@Param("typeId") Long typeId);

    /**
     * 根据编码查询数据字典记录条数
     *
     * @param code
     * @param typeId
     * @return
     */
    @Select("select count(0) from sys_dic_value where code = #{code} and type_id = #{typeId} and status = 'ABLE'")
    int queryDicValueCountByCode(@Param("code") String code, @Param("typeId") Long typeId);

    /**
     * 根据ID和编码查询数据字典记录条数
     *
     * @param id
     * @param code
     * @param typeId
     * @return
     */
    @Select("select count(0) from sys_dic_value where id = #{id} and code = #{code} and type_id = #{typeId} and status = 'ABLE'")
    int queryDicValueCountByIdAndCode(@Param("id") Long id, @Param("code") String code, @Param("typeId") Long typeId);


    /**
     * 根据字典类型查询
     *
     * @param typeCode
     * @return
     */
    @Select("SELECT\n" +
            "\tdicVal.*\n" +
            "FROM\n" +
            "\tsys_dic_value dicVal\n" +
            "LEFT JOIN sys_dic_type dicType ON dicType.id = dicVal.type_id\n" +
            "WHERE\n" +
            "\tdicType.`status` = 'ABLE'\n" +
            "AND dicVal.`status` = 'ABLE'\n" +
            "AND dicType.`code` = #{typeCode}")
    List<SysDicValue> selectByTypeCode(@Param("typeCode") String typeCode);

    /**
     * 分页查询数据字典
     *
     * @param order
     * @return
     */
    @SelectProvider(type = SqlProvider.class, method = "pageQuery")
    List<SysDicValueInfo> pageQuery(@Param("order") SysDicValuePageOrder order);

    class SqlProvider {
        public String pageQuery(@Param("order") SysDicValuePageOrder order) {
            StringBuffer sb = new StringBuffer("select * from sys_dic_value where status = 'ABLE' and type_id = #{order.typeId}");
            Boolean where = false;
            if (StringUtils.isNotBlank(order.getCode())) {
                sb.append(" and code like ('%'+#{order.code}+'%')");
            }
            if (StringUtils.isNotBlank(order.getName())) {
                sb.append(" and name like ('%'+#{order.name}+'%')");
            }
            sb.append(" order by create_time desc");
            return sb.toString();
        }
    }
}
